import urllib, csv

#tickers=['NASDAQ:GOOG', 'NASDAQ:YHOO', 'NASDAQ:MSFT', 'NYSE:UBS', 'NYSE:C']
tickers={1: ['SI', 'INTC', 'VOD', 'RBS', 'GOOG', 'YHOO'],
         2: ['ICO.L', 'VOD.L', 'RBS.L', 'BAY.L', 'BP.L', 'EON.L'],
         3: ['BAI1.F', 'BPE5.F', 'EOA.F', 'SIE.F']}

pairs=[('SI', 'SIE.F'), ('INTC', 'ICO.L'), ('VOD', 'VOD.L'), ('RBS', 'RBS.L'),
 ('BAY.L', 'BAI1.F'), ('BP.L', 'BPE5.F'), ('EON.L', 'EOA.F'), 'YHOO', 'GOOG']

stocknum=[1,2,3,4,5,6,
          2,3,4,7,8,9,
          7,8,9,1]

closevals={}

for market in tickers.keys():
    closevals[market]={}
    for ticker in tickers[market]:
        closevals[market][ticker]=[]
        #f=urllib.urlopen('http://finance.google.com/finance/historical?q=' +
        #                 ticker + '&output=csv')

        f=urllib.urlopen('http://ichart.finance.yahoo.com/table.csv?s=' + ticker +'&d=3&e=10&f=2008&g=d&a=3&b=10&c=2007&ignore=.csv')
    
        f.readline()
        for row in csv.reader(f):
            closevals[market][ticker].append(row[4])

f=open("data.csv", "wb")
writer = csv.writer(f)
for market in closevals.keys():
    for values in closevals[market].values():
        print len(values)
        
        writer.writerow([market] +[stocknum.pop(0)] + values[0:119])

f.close()
